﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NFinalCompiler.SqlModel
{
    public class ModelHelper
    {
        public static string GetName(string v)
        {
            string a = v;
            a = a.Substring(0, 1).ToUpper() + a.Substring(1);
            return a;
        }
        public static string GetPrivateName(string v)
        {
            return "_" + GetName(v).ToLower();
        }
        public static string GetTableName(DatabaseSchemaReader.DataSchema.DatabaseTable table)
        {
            string a = table.Name.Remove(0, table.Name.IndexOf("_") + 1).Replace("_", "");
            a = a.Substring(0, 1).ToUpper() + a.Substring(1);
            return a;
        }
        public static string GetCol(DatabaseSchemaReader.DataSchema.DatabaseTable table)
        {
            string sql = "";
            int i = 0;
            foreach (var m in table.Columns)
            {
                if (!m.IsPrimaryKey)
                {
                    if (i > 0)
                    {
                        sql += ",[" + m.Name + "]";
                    }
                    else
                    {
                        sql += "[" + m.Name + "]";
                    }
                    if (m.DataType.NetDataTypeCSharpName == "geography")
                    {
                        sql += ".ToString()" + m.Name;
                    }
                    i++;
                }

            }

            return sql;
        }
        public static string GetColAll(DatabaseSchemaReader.DataSchema.DatabaseTable table)
        {
            string sql = "";
            int i = 0;
            foreach (var m in table.Columns)
            {

                if (i > 0)
                {
                    sql += ",[" + m.Name + "]";
                }
                else
                {
                    sql += "[" + m.Name + "]";
                }

                i++;
            }

            return sql;
        }
        public static string GetColp(DatabaseSchemaReader.DataSchema.DatabaseTable table)
        {
            string sql = "";
            int i = 0;
            foreach (var m in table.Columns)
            {
                if (!m.IsPrimaryKey)
                {

                    if (i > 0)
                    {
                        if (m.DataType.NetDataTypeCSharpName == "geography")
                        {
                            sql += ",geography::STGeomFromText(@" + m.Name + ", 4326)";
                        }
                        else
                        {
                            sql += ",@" + m.Name;
                        }

                    }
                    else
                    {
                        if (m.DataType.NetDataTypeCSharpName == "geography")
                        {
                            sql += "geography::STGeomFromText(@" + m.Name + ", 4326)";
                        }
                        else
                        {
                            sql += "@" + m.Name;
                        }
                    }

                    i++;
                }

            }

            return sql;
        }
        public static string GetColu(DatabaseSchemaReader.DataSchema.DatabaseTable table)
        {
            string sql = "";
            int i = 0;
            foreach (var m in table.Columns)
            {
                if (!m.IsPrimaryKey)
                {
                    if (i > 0)
                    {
                        if (m.DataType.NetDataTypeCSharpName == "geography")
                        {
                            sql += "," + m.Name + "=geography::STGeomFromText(@" + m.Name + ", 4326)";
                        }
                        else
                        {
                            sql += "," + m.Name + "=@" + m.Name;
                        }

                    }
                    else
                    {
                        if (m.DataType.NetDataTypeCSharpName == "geography")
                        {
                            sql += m.Name + "=geography::STGeomFromText(@" + m.Name + ", 4326)";
                        }
                        else
                        {
                            sql += m.Name + "=@" + m.Name;
                        }

                    }

                    i++;
                }

            }

            return sql;
        }
        public static string FiledDefaultValue(DatabaseSchemaReader.DataSchema.DatabaseColumn cs)
        {
            if (cs.IsPrimaryKey) return ";";
            if (cs.DefaultValue == null)
            {
                return ";";
            }
            string def = cs.DefaultValue.Trim('(',')','\'');

            if (string.IsNullOrEmpty(def))
            {
                string v = "";
                var dbType = cs.DataType.NetDataTypeCSharpName;
                if (cs.DataType.NetDataTypeCSharpName == "geography") return v = "=\"POINT (0 0)\";";
                switch (dbType)
                {
                    case "string":
                        v = " = \"\";";
                        break;
                    case "DateTime":
                        v = " = new DateTime(1900, 1, 1);";
                        break;
                    case "TimeSpan":
                        v = " = new TimeSpan();";
                        break;
                    case "bool":
                        v = " = false;";
                        break;
                    case "decimal":
                        v = " = 0m;";
                        break;
                    default:
                        v = " = 0;";
                        break;

                }
                return v;
            }
            else
            {
                string v = "";
                var dbType = cs.DataType.NetDataTypeCSharpName;
                switch (dbType)
                {
                    case "string":
                        v = "\"" + def + "\";";
                        break;
                    case "DateTime":
                        if (def.IndexOf("getdate") >= 0)
                        {
                            v = "DateTime.Now;";
                        }
                        else
                        {
                            v = "Convert.ToDateTime(\"" + def + "\");";
                        }
                        break;
                    case "TimeSpan":
                        string[] vals = def.Split(':');
                        v = string.Format("new TimeSpan({0},{1},{2});", vals[0], vals[1], vals[2]);
                        break;
                    case "bool":
                        if (def == "0")
                        {
                            v = "false;";
                        }
                        else
                        {
                            v = "true;";
                        }
                        break;
                    case "decimal":
                        v = def + "M;";
                        break;
                    case "float":
                        v = def + "f;";
                        break;
                    default:
                        v = "" + def + ";";
                        break;

                }
                return " = " + v;
            }

        }
    }
}
